package com.cuteboy.admin.controller;

import com.cuteboy.admin.service.SysPermissionService;
import com.cuteboy.entity.SysPermission;
import com.cuteboy.entity.SysRole;
import com.cuteboy.util.Constants;
import com.cuteboy.util.ValidateUtils;
import com.cuteboy.vo.Page;
import com.cuteboy.vo.ResponseVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin/permission")
public class SysPermissionCotroller {

    @Resource
    private SysPermissionService sysPermissionService;

    @RequiresPermissions("admin:permission")
    @RequestMapping("topermissionmanage")
    public String topermissionmanage(){
        return "page/permission";
    }


    @RequiresPermissions(value = {"admin:permission:list"})
    @ResponseBody
    @RequestMapping("list")
    public ResponseVO permissionTree(@RequestParam String parentCode){
        JsonArray json = sysPermissionService.permissionTree(parentCode);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, json.toString());
    }


    @RequiresPermissions(value = {"admin:permission:list"})
    @ResponseBody
    @RequestMapping("info")
    public ResponseVO info(@RequestParam String code){
        SysPermission sysPermission = sysPermissionService.getEntity(code);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysPermission);
    }


    /**
     * 新增
     * @param entity
     * @return
     */
    @RequiresPermissions(value = {"admin:permission:save"})
    @ResponseBody
    @RequestMapping("save")
    public ResponseVO saveOrEdit(SysPermission entity){
        SysPermission sysPermission = sysPermissionService.save(entity);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysPermission);
    }

    /**
     * 编辑
     * @param entity
     * @return
     */
    @RequiresPermissions(value = {"admin:permission:update"})
    @ResponseBody
    @RequestMapping("update")
    public ResponseVO update(SysPermission entity){
        SysPermission sysPermission = sysPermissionService.update(entity);
        if (ValidateUtils.isNull(sysPermission)) {
            return new ResponseVO(Constants.ResponseVo.Code.ERROR, "父权限不存在，请重新编辑权限编码！");
        }
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, Constants.ResponseVo.msg.SUCCEESS, sysPermission);
    }

    @RequiresPermissions(value = {"admin:permission:delete"})
    @ResponseBody
    @RequestMapping("delete")
    public ResponseVO delete(@RequestParam String code){
        String result = sysPermissionService.delete(code);
        return new ResponseVO(Constants.ResponseVo.Code.SUCCEESS, result);
    }



//////////////////////////////////////////////////     登录加载      ////////////////////////////////////////////////////////

    /**
     * 加载权限菜单, 用于登录时排版左侧导航菜单
     * @param session
     * @return
     * @throws Exception
     * 这里传入的parentId是1
     */
    @ResponseBody
    @GetMapping("/loadMenuInfo")
    public String loadMenuInfo(HttpSession session, String parentCode)throws Exception{

        sysPermissionService.putTmenuOneClassListIntoSession(session);

        SysRole currentRole = (SysRole) session.getAttribute("currentRole");
        //根据当前用户的角色id和父节点id查询所有菜单及子集json
        String json = sysPermissionService.getAllMenuByParentCode(parentCode,currentRole.getId()).toString();
        //System.out.println(json);
        return json;
    }

}
